﻿using QldtSdh.GUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.DuLieu;

namespace QldtSdh.GUI.DaoTao
{
    public partial class FormQuanLyBaoLuu : BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyBaoLuu()
        {
            InitializeComponent();
            allowUpdate = false;
            InitGrid();
            LoadGrid();
            allowUpdate = true;
        }
        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colMaHocVien.DataPropertyName = "MaHocVien";
            colHoDem.DataPropertyName = "_HoDem";
            colTen.DataPropertyName = "_Ten";
            colSoBaoLuu.DataPropertyName = "SoBaoLuu";
            colNgayBaoLuu.DataPropertyName = "NgayBaoLuu";
            colNgayNhapHocLai.DataPropertyName = "NgayNhapHocLai";
            gridData.Columns[colNgayBaoLuu.Index].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridData.Columns[colNgayNhapHocLai.Index].DefaultCellStyle.Format = "dd/MM/yyyy";
        }

        void LoadGrid()
        {
            var ls = BizBaoLuu.GetList();
            bdData.DataSource = ls;
            gridData.DataSource = bdData;
            if (ls != null && ls.Count > 0)
            {
                var current = bdData.Current as BaoLuu;
                if (current != null)
                {
                    if (current.NgayNhapHocLai.HasValue)
                        btnXacNhanHocLai.Enabled = false;
                    else
                        btnXacNhanHocLai.Enabled = true;
                }
            }
        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                var current = bdData.Current as BaoLuu;
                if (current != null)
                {
                    var hocVien = BizHocVien.GetByID(current.MaHocVien);
                    if (hocVien != null)
                    {
                        HocVien_PropertyGrid pro = new HocVien_PropertyGrid
                        {
                            DiaChi = hocVien.DiaChi,
                            Email = hocVien.Email,
                            GioiTinh = hocVien.GioiTinh ? "Nam" : "Nữ",
                            HoTen = string.Format("{0} {1}", hocVien.HoDem, hocVien.Ten),
                            MaHocVien = hocVien.MaHocVien,
                            NgaySinh = hocVien.NgaySinh,
                            NoiSinh = hocVien.NoiSinh,
                            QuocTich = hocVien.QuocTich,
                            SoDienThoai = hocVien.SoDienThoai,
                        };
                        propertyGridHocVien.SelectedObject = pro;
                    }
                    else
                        propertyGridHocVien.SelectedObject = null;

                    if (current.NgayNhapHocLai.HasValue)
                        btnXacNhanHocLai.Enabled = false;
                    else
                        btnXacNhanHocLai.Enabled = true;
                }
            }
        }

        private void btnBaoLuu_Click(object sender, EventArgs e)
        {
            var dg = new FormQuanLyBaoLuu_BaoLuu();
            if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                LoadGrid();
        }

        private void btnThongTinHocVien_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as BaoLuu;
            if (current != null)
            {
                var dg = new FormThongTinChiTietHocVien(current.MaHocVien);
                dg.ShowDialog();
            }
        }

        private void btnXacNhanHocLai_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as BaoLuu;
            if (current != null)
            {
                var dg = new FormQuanLyBaoLuu_VaoHocLai(current.MaHocVien);
                if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadGrid();
            }
        }
    }
}
